<?php
// Library files
include '../includes/cDates.php';
include '../includes/cPlayer.php';
include '../includes/cMember.php';
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cPage.php';

// Globals
define("TD_POINTS", "<td width='100' align='center'>");
define("TD_NAME", "<td width='300' align='left'>");
define("TD_NORMAL", "<td width='100' align='center'>");

$cPage = new page("Team Of The Month: Previous Winners");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

// Sort dates out
$cDates = new dates();

$thisWeek = $cDates->getCurrentWeek();
$thisMonth = $cDates->getMonthFromWeek($thisWeek-1);

$startWeek = $cDates->getWeekFromDate($cDates->getStartDateForMonth($thisMonth), 'start');
$endWeek = $cDates->getWeekFromDate($cDates->getEndDateForMonth($thisMonth), 'end');

// Now sort the member scores out
$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$selectArray[0] = '*';
$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);

$finfo = mysqli_fetch_fields($result);
$numMembers = mysqli_num_rows($result);

for ($i = 0; $i < $numMembers; $i++)		// Do each member's team at a time
{
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	
	// Get all the data for this member and put it in the class
	$cMember[$i] = new member;

	$error |= $cMember[$i]->setMemberName($row['memberName']);
	$error |= $cMember[$i]->setTeamName($row['teamName']);
	$error |= $cMember[$i]->setMoneyPaid($row['moneyPaid']);
	$error |= $cMember[$i]->setActive($row['active']);
	$error |= $cMember[$i]->loadMemberPlayerInfo($row, $connect);
}

$memberMonthlyScore = array();
$memberMonthlyScoreUnsorted = array();

for ($i = 0; $i < $numMembers; $i++)
{
	for ($week = $startWeek; $week <= $endWeek; $week++)
	{
		$memberMonthlyScore[$i] += $cMember[$i]->getScoreForWeek($week);
		$memberMonthlyScoreUnsorted[$i] += $cMember[$i]->getScoreForWeek($week);
	}
}
rsort($memberMonthlyScore);		// Sort in reverse order

// We now have all the scores in order but we need to relate them to each memberTeam
for ($i = 0; $i < $numMembers; $i++)
{
	for ($j = 0; $j < sizeof($memberMonthlyScore); $j++)
	{	
		if ($memberMonthlyScoreUnsorted[$i] == $memberMonthlyScore[$j])		// We've found a match
		{
			if (!$flag)
				$cMember[$i]->setPosition(($j + 1), $thisWeek-1);	// Add one as this array starts at 0
			$flag++;
		}
	}
	$cMember[$i]->setMatchingScore($flag, $thisWeek-1);	// Indicates how many other members are on the same score
	$flag = 0;												// reset flag, it's a new member
}


// Display Scoreboard
echo "<div align='center'>";
echo "<table>";
echo TD_NORMAL."<strong>Position This Week(".($thisWeek-1).")</strong></td>";
echo TD_NAME."<strong>Team Name</strong></td>";
echo TD_NAME."<strong>Member Name</strong></td>";
echo TD_POINTS."<strong>Total Points (".$cDates->getMonthName($thisMonth).")</strong></td>";
echo TD_POINTS."<strong>Points This Week(".($thisWeek-1).")</strong></td>";
for ($position = 1; $position <= $numMembers; $position++)
{
	if ($position < 2)
		$colour = RED;
	else
		$colour = BLUE;
	// Find members from first to last being careful with members on the same points
	for ($memberIndex = 0; $memberIndex < $numMembers; $memberIndex++)
	{
		if ($cMember[$memberIndex]->getPosition($thisWeek-1) == $position)
		{	// We've found it
			echo "<tr>".TD_NORMAL.$colour.$cMember[$memberIndex]->getPosition($thisWeek-1);
			if ($cMember[$memberIndex]->getMatchingScore($thisWeek-1) > 1)
			{
				echo "=";
			}
			echo "</font></td>";
			
			echo TD_NAME.$colour.$cMember[$memberIndex]->getTeamName()."</font></td>";
			echo TD_NAME.$colour.$cMember[$memberIndex]->getMemberName()."</font></td>";
			echo TD_POINTS.$colour.$memberMonthlyScoreUnsorted[$memberIndex]/*$cMember[$memberIndex]->getMemberTotalScore()*/."</font></td>";
			echo TD_POINTS.$colour.$cMember[$memberIndex]->getScoreForWeek($thisWeek-1)."</font></td>";
			
			echo "</tr>";
		}
	}
}
echo "</table></div>";

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>